<template>
  <div class="login-container">
    <div class="login-box">
    <div class="login-left">
      <!-- 标题 -->
      <div class="title">
        欢迎使用
      </div>
      <!-- 登陆表单 -->
      <el-form ref="loginFormRef" class="login-form" :model="loginForm" :rules="loginFormRules">
        <el-form-item prop="username">
          <el-col>
            <!-- <span>账户</span> -->
          <el-input placeholder="请输入用户名" prefix-icon="User" v-model="loginForm.username">admin</el-input>
          </el-col>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" show-password prefix-icon="Lock" v-model="loginForm.password">123456</el-input>
        </el-form-item>
        <el-row>
          <el-button type="primary" class="login-btn" @click="login">登录</el-button>
        </el-row>
        <span>没有账号? 请前往
          <el-button text type="primary" @click="registerFormVisible = true"> 注册</el-button> 一下
          </span>
      </el-form>
    </div>
     <div class="box-right"></div>
     </div>
     <!-- 注册弹出框 -->
    <el-dialog v-model="registerFormVisible" title="注册" width="50%" >
    <el-form :model="registerForm" :rules="registerFormRules" ref = "refForm" >
     <el-form-item prop="userAccount" label="账户" label-width="100px">
          <el-input v-model="registerForm.userAccount"  ></el-input>
      </el-form-item>
     <el-form-item prop="password" label="密码" label-width="100px">
          <el-input v-model="registerForm.password" show-password  ></el-input>
      </el-form-item>
     <el-form-item prop="confirmpassword" label="确认密码" label-width="100px">
          <el-input v-model="registerForm.confirmpassword" show-password  ></el-input>
      </el-form-item>
     <el-form-item prop="userName" label="姓名" label-width="100px">
          <el-input v-model="registerForm.userName"  ></el-input>
      </el-form-item>
       <el-form-item  label="职位" label-width="100px">
        <el-select   v-model="registerForm.position">
          <el-option label="Frontend Developer" value="Frontend" />
          <el-option label="BackEnd Developer" value="BackEnd" />
          <el-option label="Tester" value="Tester" />
          <el-option label="Business Analysis" value="Business Analysis" /></el-select>
      </el-form-item>
      </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="registerFormVisible = false">取消</el-button>
        <el-button type="primary" @click="register">确认</el-button>
      </span>
    </template>
  </el-dialog>
  </div>
</template>

<script >
import { useRouter } from 'vue-router'
import { reactive, toRefs, ref } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
export default {
  setup() {
    const state = reactive({
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
          // { min: 6, message: '密码长度至少6个字符', trigger: 'blur' }
        ]
      },
      registerFormRules: {
        userAccount: [
          {
            required: true,
            message: '请输入账号',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 16,
            message:
            '密码长度不少于6位,必须至少包含一个大小写字母、数字、特殊符号',
            trigger: 'blur',
            pattern:
            '^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#$%*--+/\\\\.])[\\da-zA-Z!@#$%*--+/\\\\.]{6,16}$'
          }
        ],
        confirmpassword: [
          {
            required: true,
            message: '请再次输入密码',
            trigger: 'blur'
          },
          {
            min: 6,
            message: '密码不一致，请重新输入',
            trigger: 'blur',

            pattern:
            '^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#$%*--+/\\\\.])[\\da-zA-Z!@#$%*--+/\\\\.]{6,}$'
          }
        ]

      }
    })
    const router = useRouter()
    // 登录按钮点击事件
    const login = () => {
      getUser()
    }
    async function getUser() {
      // const { data: res } = await axios.get('/user/get')
      const { data: res } = await axios.post('/user/login',
        {
          userAccount: state.loginForm.username,
          password: state.loginForm.password
        })
      console.log(res)
      console.log(state.loginForm.username)
      if (res.code !== 200) {
        return ElMessage({
          message: '请检查用户名和密码！',
          type: 'error'
        })
      }
      ElMessage({
        message: '登陆成功！',
        type: 'success'
      })
      // 登陆成功后将用户信息保存到sessionStorage中 并将信息转换为字符串
      sessionStorage.setItem('userInfo', JSON.stringify(res.data))
      router.push({ path: '/home' })
      console.log(res)
    }
    // 注册
    const registerFormVisible = ref(false)
    // const registerState = {
    const refForm = ref(null)
    const registerForm = reactive({
      password: '',
      userName: '',
      userAccount: '',
      confirmpassword: '',
      position: ''
    })
    // registerFormRules: []

    // }
    const register = () => {
      refForm.value.validate(async (valid) => {
        if (!valid) {
          return
        }
        const { data: res } = await axios.post('/user/add', registerForm)
        if (res.code !== 200) {
          return ElMessage({
            message: res.message,
            type: 'error'
          })
        }
        ElMessage({
          message: '注册成功',
          type: 'success'
        })
        state.loginForm.username = registerForm.userAccount
        console.log(res)
        registerFormVisible.value = false
      })
    }

    return {
      ...toRefs(state),
      login,
      getUser,
      // 注册
      refForm,
      registerFormVisible,
      // registerState,
      registerForm,
      register

    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background-color: #12abdb;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
.login-box{
  display:flex;
  height: 400px;
  width: 800px;
  border-radius: 50px;
  background-color: #fff;

  .login-left {
    height:100%;
    width:500px;
    position: relative;
    .title {
      position: absolute;
      top: 30px;
      left: 50%;
      transform: translate(-50%);
      font-size: 24px;
    }
  }
  .box-right{
      height:100%;
      width:300px;
      background-color:#0070ad;
      border-radius: 0 50px 50px 0;

    }
  .login-form {
    margin-top: 150px;
    width: 80%;
    left: 50%;
    transform: translate(-50%);
    position: absolute;
    span{
      margin-top:30px;
      position: absolute;
      font-size:12px;
      left: 50%;
      transform: translate(-50%);
    }
  }
  .login-btn {
    width: 100%;
  }
}
}
</style>
